package com.zhanqiao.controller;

import com.google.gson.Gson;
import com.zhanqiao.service.UpdateDraftService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/publishcontent.action")
public class UpdateDraftController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String publishdate = req.getParameter("publish");
        resp.setContentType("application/json;charset=utf8");
        UpdateDraftService updateDraftService = new UpdateDraftService();
        int i = updateDraftService.articleService(id, content, publishdate, title);
        Map message = new HashMap();
        if(i==0){
            message.put("msg","发送失败");
            String result = new Gson().toJson(message);
            resp.getWriter().write(result);
        }else{
            message.put("msg","发送成功");
            String result = new Gson().toJson(message);
            resp.getWriter().write(result);
        }
    }
}
